package com.viewpagerindicator;

import android.content.Context;
import android.support.v4.view.MotionEventCompat;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.HorizontalScrollView;

public class CustomHorizontalScrollView extends HorizontalScrollView {

    private OnScrollToRightListener scrollToRightListener;

    public void setScrollToRightListener(OnScrollToRightListener scrollToRightListener) {
        this.scrollToRightListener = scrollToRightListener;
    }

    public CustomHorizontalScrollView(Context context) {
        super( context);
    }
    public CustomHorizontalScrollView(Context context, AttributeSet attrs) {
        super( context, attrs);
    }
    public CustomHorizontalScrollView(Context context, AttributeSet attrs, int defStyle) {
        super( context, attrs, defStyle);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        int action = MotionEventCompat.getActionMasked( ev);
        switch(action) {
            case MotionEvent.ACTION_DOWN:
                if (canScrollHorizontallyCompat( -1)) {
                    if (getParent() != null) {
                        getParent().requestDisallowInterceptTouchEvent( true);
                    }
                }
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                if (getParent() != null) {
                    getParent().requestDisallowInterceptTouchEvent( false);
                }
            default:
                break;
        }
        return super.onInterceptTouchEvent( ev);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (scrollToRightListener != null) {
            scrollToRightListener.onScrollToRight(computeHorizontalScrollRange() - computeHorizontalScrollExtent() <= computeHorizontalScrollOffset());
        }
    }

    public interface OnScrollToRightListener {
        void onScrollToRight(boolean isToRight);
    }
    
    /**
     * Check if this view can be scrolled horizontally in a certain direction.
     * <br>FIX: {@link ViewCompat#canScrollHorizontally(android.view.View, int)} return false directly 
     *  when OS version is below 14.
     * @param direction Negative to check scrolling left, positive to check scrolling right.
     * @return true if this view can be scrolled in the specified direction, false otherwise.
     * @see ViewCompat#canScrollHorizontally(android.view.View, int)
     */
    private boolean canScrollHorizontallyCompat(int direction) {
        final int version = android.os.Build.VERSION.SDK_INT;
        if (version >= 14) {
            return ViewCompat.canScrollHorizontally( this, direction);
        }
        else {
            final int offset = computeHorizontalScrollOffset();
            final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
            if (range == 0) {
                return false;
            }
            if (direction < 0) {
                return offset > 0;
            } else {
                return offset < range - 1;
            }
        }
    }
}
